package com.wlc.javasggmianshiti2022.sgg01.episode2.two;

import java.util.concurrent.*;

/**
 * @author 王立朝
 * @date 2022/11/22
 * @description:
 */
public class TestSingle02 {

    public static void test01() throws InterruptedException {
        Single02 instance = Single02.getInstance();
        Single02 instance2 = Single02.getInstance();
        System.out.println("instance == instance2 = " + (instance == instance2));
        System.out.println("instance = " + instance);
        System.out.println("instance2 = " + instance2);
    }

    public static void test02() throws ExecutionException, InterruptedException {
        // 使用多线程来获取单例对象，验证这个单例对象是不是一样的
        Callable<Single02> ca = new Callable<Single02>() {
            @Override
            public Single02 call() throws Exception {
                return Single02.getInstance();
            }
        };
        ExecutorService es = Executors.newFixedThreadPool(2);
        Future<Single02> f1 = es.submit(ca);
        Future<Single02> f2 = es.submit(ca);

        Single02 s1 = f1.get();
        Single02 s2 = f2.get();
        System.out.println("s1 = " + s1);
        System.out.println("s2 = " + s2);
        System.out.println("s1==s2 = " + (s1 == s2));
        es.shutdown();

    }
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        TestSingle02.test02();
    }

}
